/**
 * @file    addbuddy.cpp
 * @author  Siddharth Sathyam <ssathy2@gmail.com>
 * @author  Alan Ruvalcaba <aruval3@gmail.com>
 * @author  Pat LaMontagna <plamontagna@gmail.com>
 * @version 1.0
 * @section LICENSE
 *
 * This program is property of Siddharth Sathyam, Alan Ruvalcaba, Pat LaMontagna.
 *
 * @section DESCRIPTION
 *
 * The addBuddy class is a dialog that allows users to add valid friends to their friendslist.
 */

#include "addbuddy.h"
#include "ui_addbuddy.h"

addBuddy::addBuddy(QWidget *parent, QString personSending):
    QDialog(parent),
    ui(new Ui::addBuddy)
{
    ui->setupUi(this);
    personSendingRequest = personSending;
    ui->addButton->setDefault(true);
}

addBuddy::~addBuddy()
{
    delete ui;
}

void addBuddy::handleIsValidFriendRequestConfirmation(QString personSentTo, bool isConfirmed)
{
    if(isConfirmed)
    {
	displayMessage("Friend Request was successfully sent to: " + personSentTo);
    }
    else if(!isConfirmed)
    {
	displayMessage("Invalid Friend Request...Request was not sent");
	this->show();
    }
}

void addBuddy::displayMessage(QString msgToDisplay)
{
    QMessageBox* message = new QMessageBox(0);
    message->setText(msgToDisplay);
    message->setWindowTitle("Add Buddy");
    message->setAttribute(Qt::WA_DeleteOnClose);
    message->show();
}

void addBuddy::on_addButton_clicked()
{
    QString userInput = ui->lineEdit->text();
    // The user name that the user is trying to add has spaces
    if(!userInput.contains(" "))
    {
	// Making sure the username isn't the person's own username and it's not blank
	if(userInput != "" && userInput != personSendingRequest)
	{
	    emit addFriend(userInput);
	}
	else
	{
	    displayMessage("Invalid User Name...Please enter a valid friend to add");
	    this->show();
	}
    }
    else
    {
	displayMessage("Friend Names can't have spaces in them");
	this->show();
    }
}

void addBuddy::on_cancelButton_clicked()
{
    this->close();
}
